package servlet.assitant;

import com.google.gson.Gson;
import dao.domain.Sell;
import service.AssistantService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author huangzhiyang
 * @Date 2023/10/17
 * @Description ${PACKAGE_name}
 */
@WebServlet(name = "AssistantGetSellServlet", value = "/AssistantGetSellServlet")
public class AssistantGetSellServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Sell> sells;
        try {
            sells = AssistantService.listSell();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        Gson gson = new Gson();
        String jsonData = gson.toJson(sells);
        System.out.println(jsonData);
        response.setContentType("application/json");
        response.getWriter().write(jsonData);
        System.out.println("finish");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("AssistantGetSellServlet现在来了一个post请求，将要去走get的代码了。");
        doGet(request, response);
    }
}
